#!/usr/bin/env python
# encoding: utf-8


"""
@version : v4.5
@author  : 
@license : 
@contact : ****@massclouds.com
@site    : http://blog.csdn.net/***
@software: PyCharm
@file    : api-test.py
@time    : 16-10-10 上午11:36
"""

import ssl
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager

class Ssl3HttpAdapter(HTTPAdapter):
    """"Transport adapter" that allows us to use SSLv3."""
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_SSLv3)
                                       # ssl_version=ssl.PROTOCOL_SSLv23)

def test():
    import requests,json
    ip = "192.168.8.13"
    port = 8000
    url = "https://%s:%s/login" % (ip,port)

    payload = {
        "username": "jc",
        "password": "123456",
        "eauth": "pam"  # vmh
    }
    # import urllib3.contrib.pyopenssl
    # urllib3.contrib.pyopenssl.inject_into_urllib3()

    headers = {'Content-Type': 'application/x-www-form-urlencoded',
                     'Accept': 'application/json'}
    print url


    s = requests.Session()
    s.mount(url,Ssl3HttpAdapter())

    s.post(
            url, data=json.dumps(payload), headers=headers,
                              cert=("/tmp/localhost.crt","/tmp/localhost_nopass.key"),
                              verify=True
            )
    # recv = requests.get(url,verify=False)
    # recv = requests.get(url,verify=True,cert=("/tmp/test.cer","/tmp/key"))
    # recv = requests.post(url, data=json.dumps(payload), headers=headers, verify=False,timeout=10,
    #                      cert=("/tmp/test.cer","/tmp/key")
    #                      )
    # recv = requests.post(url, data=json.dumps(payload), headers=headers,
    #                      verify=True,
    #                       cert=("/tmp/localhost.crt","/tmp/localhost_nopass.key"))
                         #    verify=False)
                         #    )
                         # )


    # recv.raise_for_status()     # 如果recv.status_code状态码不是 200，就主动抛出异常
    # recv_result = recv.json()   # 转换　json ==> dict
    # print recv_result


if __name__ == '__main__':
    # try:
    #     test()
    # except Exception as e:
    #     print e.message
    # pass
    test()
